#include "fun.h"


struct fr_list        *root_list=NULL;
static struct fr_list *actual_list=NULL;
static int            id=1;


/*this function insert new element to linked list of frames */
struct fr_list*
ins_fr ( u_int16_t th_sport,
		 u_int16_t th_dport,
		 struct ether_header  *eth_fr){

  struct fr_list *tmp;
  
  
  if(!(tmp=(struct fr_list *)malloc(sizeof(struct fr_list)))){
	printf("Cannot Malloc Memory for fr_list structure\n");
	exit(1);
  }


  /*If this element is first so I need to create special root node*/
  if(root_list==NULL){
	root_list=tmp;
	actual_list=tmp;
	
	tmp->n_e=NULL;/*Next element null*/

	tmp->th_sport=th_sport;/*set sport and dport to recommended value */
	tmp->th_dport=th_dport;

	/*I copy dest a source mac address to my struct*/
	bcopy(eth_fr->ether_dhost,tmp->ether_dhost,6);
	bcopy(eth_fr->ether_shost,tmp->ether_shost,6);

	tmp->id=id++;
  }
  else{
	actual_list->n_e=tmp; /*insert new element into the list */

	tmp->n_e=NULL;/*Next element null*/

	tmp->th_sport=th_sport;/*set sport and dport to recommended value */
	tmp->th_dport=th_dport;
	
	/*I copy dest a source mac address to my struct*/
	bcopy(eth_fr->ether_dhost,tmp->ether_dhost,6);
	bcopy(eth_fr->ether_shost,tmp->ether_shost,6);

	tmp->id=id++;
	
	actual_list=tmp; /*set new actual_list to actual end of list */
	
  }
  return tmp;
}

/*this function search linked list of frames for given ID a returns pointer to this structure */
struct fr_list*
s_fr_list_id (int s_id){

  struct fr_list *tmp;

  if(s_id==-1)
	return NULL;
  
  tmp=root_list;
  
  while(tmp!=NULL){
	if(tmp->id==s_id)
	  return tmp;
	else
	  tmp=tmp->n_e;
	/*	free(tmp);
		tmp=tmp1;*/
  }
  return NULL;
}

